package 程序自动分析;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class Main {
    static int N = 100010, u, n;
    static HashMap<Integer, Integer> map = new HashMap<>();
    static int[] p = new int[2 * N];
    static PIII[] q = new PIII[N];

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(in.readLine());
        while (T-- > 0) {
            u = 0;
            map.clear();
            int n = Integer.parseInt(in.readLine());
            for (int i = 1; i <= 2 * n; i++) {
                p[i] = i;
            }
            for (int i = 1; i <= n; i++) {
                String[] s = in.readLine().split(" ");
                int a = Integer.parseInt(s[0]);
                int b = Integer.parseInt(s[1]);
                int c = Integer.parseInt(s[2]);
                q[i] = new PIII(a, b, c);
            }
            for (int i = 1; i <= n; i++) {
                if (q[i].z == 1) {
                    int a = get(q[i].x);
                    int b = get(q[i].y);
                    p[find(a)] = find(b);
                }
            }
            boolean falg = false;
            for (int i = 1; i <= n; i++) {
                if (q[i].z == 0) {
                    int a = get(q[i].x);
                    int b = get(q[i].y);
                    if (find(a) == find(b)) {
                        falg = true;
                        break;
                    }
                }
            }
            if (!falg) {
                System.out.println("YES");
            }else {
                System.out.println("NO");
            }
        }
    }

    private static int get(int x) {
        if (!map.containsKey(x)) {
            map.put(x, ++u);
        }
        return map.get(x);
    }

    private static int find(int x) {
        if (x != p[x]) {
            p[x] = find(p[x]);
        }
        return p[x];
    }
}

class PIII {
    int x, y, z;

    public PIII(int x, int y, int z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
}
